package com.hsy.linemoney.apiservice;

import android.os.AsyncTask;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.beans.User;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 我的代理相关
 * Created by MartinKent on 2016/10/26.
 */

public class AgentService extends BasicService<JSONArray> {
//    我所属机构的点数汇总 - 获取卷起点数
    public void getPointRollUpRatio(final Callback<PointRollUpRatioResult> callback) {
        final String api = "/LmAgentPoint-portlet.userpointtotal/get-point-roll-up-ratio";
        final JSONObject params = new JSONObject();
        new AsyncTask<Object, Object, Object>() {

            @Override
            protected Object doInBackground(Object... objects) {
                try {
                    JSONObject cmd = new JSONObject();
                    cmd.put(api, params);
                    JSONArray array = LineMoneyContext.getSession().invoke(cmd);
                    DPLogUtil.d(array.toString());
                    return new PointRollUpRatioResult(array.getJSONObject(0));
                } catch (JSONException e) {
                    e.printStackTrace();
                    return e;
                } catch (Exception e) {
                    e.printStackTrace();
                    return e;
                }
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                if (null != callback) {
                    if (o instanceof PointRollUpRatioResult) {
                        callback.onSuccess(0, (PointRollUpRatioResult) o);
                    } else {
                        Exception e = (Exception) o;
                        callback.onError(0, 0, e.getLocalizedMessage(), e);
                    }
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    public static class PointRollUpRatioResult {
        private List<Level> levels;

        public PointRollUpRatioResult(JSONObject object) {
            levels = new ArrayList<>();
            Iterator<String> keys = object.keys();
            while (keys.hasNext()) {
                String k = keys.next();
                try {
                    JSONObject o = object.getJSONObject(k);
                    Level level = DPGsonUtil.fromJson(o.toString(), Level.class);
                    levels.add(level);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        public List<Level> getLevels() {
            return levels;
        }

        public void setLevels(List<Level> levels) {
            this.levels = levels;
        }

        public static class Level {
            private String activeFlag;

            private String addTime;

            private String addUser;

            private String level;

            private String maxLevel;

            private double ratio;

            private String rollUpId;

            private String updateTime;

            private String updateUser;

            public void setActiveFlag(String activeFlag) {
                this.activeFlag = activeFlag;
            }

            public String getActiveFlag() {
                return this.activeFlag;
            }

            public void setAddTime(String addTime) {
                this.addTime = addTime;
            }

            public String getAddTime() {
                return this.addTime;
            }

            public void setAddUser(String addUser) {
                this.addUser = addUser;
            }

            public String getAddUser() {
                return this.addUser;
            }

            public void setLevel(String level) {
                this.level = level;
            }

            public String getLevel() {
                return this.level;
            }

            public void setMaxLevel(String maxLevel) {
                this.maxLevel = maxLevel;
            }

            public String getMaxLevel() {
                return this.maxLevel;
            }

            public void setRatio(double ratio) {
                this.ratio = ratio;
            }

            public double getRatio() {
                return this.ratio;
            }

            public void setRollUpId(String rollUpId) {
                this.rollUpId = rollUpId;
            }

            public String getRollUpId() {
                return this.rollUpId;
            }

            public void setUpdateTime(String updateTime) {
                this.updateTime = updateTime;
            }

            public String getUpdateTime() {
                return this.updateTime;
            }

            public void setUpdateUser(String updateUser) {
                this.updateUser = updateUser;
            }

            public String getUpdateUser() {
                return this.updateUser;
            }
        }
    }

//    我所属机构的点数汇总 - 获取所有Level总点数
    public void getOrgPointForAllLevel(final Callback<OrgPointForAllLevelResult> callback) {
        final String api = "/LmAgentPoint-portlet.userpointtotal/get-org-point-for-all-level";
        User user = LineMoneyContext.getLoggedUser();
        final String agentOrgName = user.getUserId() + "-" + user.getFirstName() + "-Team";
        final JSONObject params = ParamsMaker.newInstance()
                .add("agentOrgName", agentOrgName)
                .get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    if (null != callback) {
                        callback.onSuccess(what, DPGsonUtil.fromJson(result.get(0).toString(), OrgPointForAllLevelResult.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class OrgPointForAllLevelResult {
        private Status status;

        private List<Value> value;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setValue(List<Value> value) {
            this.value = value;
        }

        public List<Value> getValue() {
            return this.value;
        }

        public class Value {
            private int orgId;

            private int level;

            private int userId;

            private int points;

            public void setOrgId(int orgId) {
                this.orgId = orgId;
            }

            public int getOrgId() {
                return this.orgId;
            }

            public void setLevel(int level) {
                this.level = level;
            }

            public int getLevel() {
                return this.level;
            }

            public void setUserId(int userId) {
                this.userId = userId;
            }

            public int getUserId() {
                return this.userId;
            }

            public void setPoints(int points) {
                this.points = points;
            }

            public int getPoints() {
                return this.points;
            }

        }

        public class Status {
            private String message;

            private String code;

            public void setMessage(String message) {
                this.message = message;
            }

            public String getMessage() {
                return this.message;
            }

            public void setCode(String code) {
                this.code = code;
            }

            public String getCode() {
                return this.code;
            }

        }
    }
}
